<?php
/**
 * Created by PhpStorm.
 * User: cy
 * Date: 18-4-22
 * Time: 下午8:22
 */

//创建Server对象，监听 127.0.0.1:9501端口
$serv = new swoole_server("127.0.0.1", 9501);

//根据硬件条件设定参数
$serv->set([
    'worker_num'=>2,//worker 进程数，和cpu挂钩
    'max_request'=>'10000'
]);

/**
 * 监听连接进入事件
 * $fd 客户端链接的唯一标示
 * $reactor_id 进程id
 */
$serv->on('connect', function ($serv, $fd,$reactor_id) {
    echo "客户端($fd):线程:$reactor_id,连接上来了.\n";
});

//监听数据接收事件
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, "服务器响应: $data,客户端标识：$fd,进程：$from_id.\n");
});

//监听连接关闭事件
$serv->on('close', function ($serv, $fd) {
    echo "客户端($fd): 断开了连接.\n";
});

//启动服务器
$serv->start();